package com.abu.edustatistic.service.impl;

import com.abu.edustatistic.client.UCenterClient;
import com.abu.edustatistic.pojo.StatisticsDaily;
import com.abu.edustatistic.mapper.StatisticsDailyMapper;
import com.abu.edustatistic.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Random;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author 阿布
 * @since 2020-06-25
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {


    @Resource
    private StatisticsDailyMapper statisticsDailyMapper;
    @Resource
    private UCenterClient uCenterClient;

    @Override
    public Integer saveSta(String dateString) {

//        1.先查询当天日期有多少条
        Integer count = this.uCenterClient.queryCountByDate(dateString);
//        2.如果有相同日期则 先将相同日期的数据进行删除
        this.statisticsDailyMapper.delete(new QueryWrapper<StatisticsDaily>().eq("date_calculated", dateString));
//        3.再将最新数据添加至数据库中
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily
                .setRegisterNum(count)      //  这里只进行了对某天用户注册数量的统计
                .setCourseNum(new Random().nextInt(100) + 1)     //以下是模拟数据
                .setLoginNum(new Random().nextInt(200) + 1)
                .setDateCalculated(dateString)
                .setVideoViewNum(new Random().nextInt(100) + 1);
        return this.statisticsDailyMapper.insert(statisticsDaily);
    }
}
